package com.abc.easyclaim;

import java.util.Calendar;

import com.abc.bean.Case;
import com.abc.db.DBManager;
import com.abc.tool.Constants;

import android.os.Bundle;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.InputType;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

public class CaseNewActivity extends Activity implements OnClickListener,OnTouchListener{
	private String [] brands = {"AUDI","BMW","BUICK","CADILLAC","FERRARI","INFINITI","LEXUS","MAZDA","MG","MINI","TAXI","OTHER","TOYATA","VW"};
	private DBManager mgr;
	private boolean isEdit;
	private Case caze = new Case();
	
	//字段
	private TextView titleTextView;
	private EditText nameEditText;
	private EditText accidentDateEditText;
	private EditText addressEditText;
	private EditText carNumberEditText;
	private EditText carBrandEditText;
	private EditText carOwnerNameEditText;
	private EditText carOwnerPhoneEditText ;
	private EditText carOwnerAddressEditText;
	private EditText insyranceNumberEditText;
	private EditText officerEditText;
	private EditText officerNumberEditText;
	private EditText noteEditText;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mgr = new DBManager(this);
		setContentView(R.layout.activity_case_new);
		Button submitBtn = (Button) findViewById(R.id.new_case_save);
		submitBtn.setOnClickListener(this);
		Button backBtn = (Button) findViewById(R.id.new_case_back);
		backBtn.setOnClickListener(this);
		titleTextView = (TextView) findViewById(R.id.case_new_title);
		//初始化编辑界面
		nameEditText = (EditText) findViewById(R.id.new_case_name);
		accidentDateEditText = (EditText) findViewById(R.id.new_case_accident_date);
		accidentDateEditText.setOnTouchListener(this);
		addressEditText = (EditText) findViewById(R.id.new_case_address);
		carNumberEditText = (EditText) findViewById(R.id.new_case_car_number);
		carBrandEditText = (EditText) findViewById(R.id.new_case_car_brand);
		carOwnerNameEditText = (EditText) findViewById(R.id.new_case_car_owner_name);
		carOwnerPhoneEditText = (EditText) findViewById(R.id.new_case_car_owner_phone);
		carOwnerAddressEditText = (EditText) findViewById(R.id.new_case_car_owner_address);
		insyranceNumberEditText = (EditText) findViewById(R.id.new_case_insurance_number);
		officerEditText = (EditText) findViewById(R.id.new_case_officer);
		officerNumberEditText = (EditText) findViewById(R.id.new_case_officer_number);
		noteEditText = (EditText) findViewById(R.id.new_case_note);
		
		carBrandEditText.setOnTouchListener(this);
		//判断是否是编辑模式
		Intent intent=getIntent(); 
        isEdit=intent.getBooleanExtra("isEdit", false);
        if(isEdit){
        	titleTextView.setText("编辑案例");
        	caze = mgr.queryCase(MainApplication.getCurrentCaseId());
        	nameEditText.setText(caze.getName());
    		accidentDateEditText.setText(caze.getAccidentDate());
        	addressEditText.setText(caze.getAccidentAddress());
        	carNumberEditText.setText(caze.getCarNumber());
        	carBrandEditText.setText(caze.getCarBrand());
        	carOwnerNameEditText.setText(caze.getCarOwnerName());
        	carOwnerPhoneEditText.setText(caze.getCarOwnerPhone());
        	carOwnerAddressEditText.setText(caze.getCarOwnerAddr());
        	insyranceNumberEditText.setText(caze.getInsuranceNumber());
        	officerEditText.setText(caze.getOfficerName());
        	officerNumberEditText.setText(caze.getOfficerNumber());
        	noteEditText.setText(caze.getNote());
        }
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.case_edit, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id==R.id.new_case_save){
			getData();
			//创建广播
			Intent intent = new Intent();  
	        intent.setAction(Constants.INTENT_REFRESH_CASE_LIST);  
	        sendBroadcast(intent);  
			finish();
		}else if (id == R.id.new_case_back) {
			finish();
		}
	}
	
	private void getData(){
		caze.setName(nameEditText.getText().toString().trim());
		caze.setAccidentDate(accidentDateEditText.getText().toString().trim());
		caze.setAccidentAddress(addressEditText.getText().toString().trim());
		caze.setCarNumber(carNumberEditText.getText().toString().trim());
		caze.setCarBrand(carBrandEditText.getText().toString().trim());
		caze.setCarOwnerName(carOwnerNameEditText.getText().toString().trim());
		caze.setCarOwnerPhone(carOwnerPhoneEditText.getText().toString().trim());
		caze.setCarOwnerAddr(carOwnerAddressEditText.getText().toString().trim());
		
		caze.setInsuranceNumber(insyranceNumberEditText.getText().toString().trim());
		caze.setOfficerName(officerEditText.getText().toString().trim());
		caze.setOfficerNumber(officerNumberEditText.getText().toString().trim());
		caze.setNote(noteEditText.getText().toString().trim());
		//
		if(isEdit){
			mgr.modifyCase(caze);
		}else{
			mgr.addCase(caze);
		}

	}
	
	@Override  
    protected void onDestroy() {  
        super.onDestroy();  
        //应用的最后一个Activity关闭时应释放DB  
        mgr.closeDB();  
    }

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int id =v.getId();
		if (event.getAction() == MotionEvent.ACTION_DOWN) {  
			if(v.getId() == R.id.new_case_accident_date){  
            AlertDialog.Builder builder = new AlertDialog.Builder(this);  
            View view = View.inflate(this, R.layout.date_time_dialog, null);  
            final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);  
            final TimePicker timePicker = (android.widget.TimePicker) view.findViewById(R.id.time_picker);  
            builder.setView(view);  
  
            Calendar cal = Calendar.getInstance();  
            cal.setTimeInMillis(System.currentTimeMillis());  
            datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null);  
  
            timePicker.setIs24HourView(true);  
            timePicker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));  
            timePicker.setCurrentMinute(Calendar.MINUTE);  
  
 
                final int inType = accidentDateEditText.getInputType();  
                accidentDateEditText.setInputType(InputType.TYPE_NULL);  
                accidentDateEditText.onTouchEvent(event);  
                accidentDateEditText.setInputType(inType);  
                accidentDateEditText.setSelection(accidentDateEditText.getText().length());  
                  
                builder.setTitle("选取起始时间");  
                builder.setPositiveButton("确  定", new DialogInterface.OnClickListener() {  
  
                    @Override  
                    public void onClick(DialogInterface dialog, int which) {  
  
                        StringBuffer sb = new StringBuffer();  
                        sb.append(String.format("%d-%02d-%02d",   
                                datePicker.getYear(),   
                                datePicker.getMonth() + 1,  
                                datePicker.getDayOfMonth()));  
                        sb.append("  ");  
                        sb.append(timePicker.getCurrentHour())  
                        .append(":").append(timePicker.getCurrentMinute());  
  
                        accidentDateEditText.setText(sb);   
                          
                        dialog.cancel();  
                    }  
                });    

            Dialog dialog = builder.create();  
            dialog.show();  
			}
			else if(v.getId() == R.id.new_case_car_brand){
				new AlertDialog.Builder(this)
				.setTitle("请选择")
				.setIcon(android.R.drawable.ic_dialog_info)                
				.setSingleChoiceItems(brands, 0, 
				  new DialogInterface.OnClickListener() {
				                            
				     public void onClick(DialogInterface dialog, int which) {
				         carBrandEditText.setText(brands[which]); 
				    	 dialog.dismiss();
				     }
				  }
				)
				.setNegativeButton("取消", null)
				.show();
			}
		}
		return false;
	}  
}
